<template>
 <view>
     <view class="goods-list">
       <view v-for="(goods, i) in goodsList" :key="i"  @click="gotoDetail(goods)">
         <my-goods :goods="goods"></my-goods>
       </view>
     </view>
   </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        isloading: false,
        defaultPic: 'https://img3.doubanio.com/f/movie/8dd0c794499fe925ae2ae89ee30cd225750457b4/pics/movie/celebrity-default-medium.png',
        //请求参数
        queryObject: {
          query: '',
          cid: '',
          pagesize: 10,
          pagenum: 1
        },
        goodsList: [],
        total: 0
      };
    },
    onLoad(options) {
      this.queryObject.query = options.query || ''
      this.queryObject.cid = options.cid || ''
      this.getGoodsList()
    },
    methods: {
      async getGoodsList(cb) {
        this.isloading=true
        const {
          data: res
        } = await uni.$http.get('https://api-hmugo-web.itheima.net/api/public/v1/goods/search', this.queryObject)
        if (res.meta.status !== 200) return uni.$showMsg()
        this.isloading=false
         cb && cb()
        this.goodsList = [...this.goodsList, ...res.message.goods]
        this.total = res.message.total
      },
      async gotoDetail(goods){
         uni.navigateTo({
           url: "/subpkg/goods_detail/goods_detail?goods_id="+goods.goods_id
         })
       },
      onReachBottom(){
        if(this.queryObject.pagesize *this.queryObject.pagenum>=this.total) return uni.$showMsg('数据已加载完毕')
        if(this.isloading===true){
          return
        }
        this.queryObject.pagenum +=1
        this.getGoodsList()
      },
      onPullDowneRefresh(){
        this.queryObject.pagenum=1
        this.total=0
        this.isloading=false
        this.goodsList=[]
        
        this.goodsList(()=>uni.stopPullDownRefresh())
      }
    
    }
  }
</script>

<style lang="scss">
.goods-item {
  display: flex;
  padding: 10px 5px;
  border-bottom: 1px solid #f0f0f0;

  .goods-item-left {
    margin-right: 5px;

    .goods-pic {
      width: 100px;
      height: 100px;
      display: block;
    }
  }

  .goods-item-right {
    display: flex;
    flex-direction: column;
    justify-content: space-between;

    .goods-name {
      font-size: 13px;
    }

    .goods-price {
      font-size: 16px;
      color: #c00000;
    }
  }
}
</style>
